export default {

    /**
     * 成功提示
     * @param {*} msg 
     * @param {*} time 
     */
    success(Objcet) {
        let toastBox = document.createElement('div');
        let icon = document.createElement('span');
        let content = document.createElement('p');
        let mask = document.createElement('div');
        toastBox.className = 'ep-success';
        mask.className = 'ep-mask';
        icon.className = 'ep-icon-success';
        content.innerHTML = Objcet.content;
        toastBox.appendChild(icon);
        toastBox.appendChild(content);
        document.body.appendChild(mask);
        document.body.appendChild(toastBox);
        return new Promise((resolve, reject) => {
            let timer = setTimeout(() => {
                clearTimeout(timer);
                let timer2 = setTimeout(() => {
                    document.body.removeChild(toastBox);
                    document.body.removeChild(mask);
                    clearTimeout(timer2);
                    resolve(true);
                }, 200);
            }, Objcet.duration || 3000);
        });
    },

    /**
     * 失败提示
     * @param {*} msg 
     * @param {*} time 
     */
    fail(Objcet) {
        let toastBox = document.createElement('div');
        let icon = document.createElement('span');
        let content = document.createElement('p');
        let sub = document.createElement('p');
        let mask = document.createElement('div');
        let cancel = document.createElement('button');
        toastBox.className = 'ep-fail';
        mask.className = 'ep-mask';
        icon.className = 'ep-icon-fail';
        sub.className = 'ep-sub';
        cancel.className = 'ep-messgebox-cancel ep-border-top';
        cancel.innerHTML = '关闭';
        content.innerHTML = Objcet.content || '';
        sub.innerHTML = Objcet.sub || '';
        toastBox.appendChild(icon);
        toastBox.appendChild(content);
        toastBox.appendChild(sub);
        toastBox.appendChild(cancel);
        document.body.appendChild(mask);
        document.body.appendChild(toastBox);
        return new Promise((resolve, reject) => {
            cancel.onclick = function () {
                document.body.removeChild(toastBox);
                document.body.removeChild(mask);
                resolve(true);
            };
        });
    },

    /**
     * 警告提示
     * @param {*} title 
     * @param {*} text 
     */
    alert(Objcet) {
        if (typeof Objcet == 'object') {
            let title = Objcet.title || '';
            let box = document.createElement('div');
            let titleBox = document.createElement('div');
            let body = document.createElement('div');
            let foot = document.createElement('div');
            let confirm = document.createElement('button');
            let mask = document.createElement('div');
            titleBox.innerHTML = title;
            body.innerHTML = Objcet.text || '';
            confirm.innerHTML = Objcet.confirmButtonText || '确定';
            box.className = 'ep-messgebox';
            titleBox.className = 'ep-messgebox-hd';
            body.className = 'ep-messgebox-bd ep-border-bottom';
            foot.className = 'ep-messgebox-fd';
            confirm.className = 'ep-messgebox-btn ep-messgebox-confirm';
            mask.className = 'ep-mask';
            document.body.appendChild(box);
            document.body.appendChild(mask);
            if (title !=  '') {
                box.appendChild(titleBox);
            }
            box.appendChild(body);
            box.appendChild(foot);
            foot.appendChild(confirm);
            return new Promise((resolve, reject) => {
                confirm.onclick = function () {
                    document.body.removeChild(box);
                    document.body.removeChild(mask);
                    resolve(true);
                };
            });
        }
    },

    /**
     * 弹出确认框
     * @param {*} title 
     * @param {*} text 
     */
    confirm(title = '提示', text = '', cancelButtonText = '取消', confirmButtonText = '确定') {
        let box = document.createElement('div');
        let titleBox = document.createElement('div');
        let body = document.createElement('div');
        let foot = document.createElement('div');
        let cancel = document.createElement('button');
        let confirm = document.createElement('button');
        let mask = document.createElement('div');
        titleBox.innerHTML = title;
        body.innerHTML = text;
        cancel.innerHTML = cancelButtonText;
        confirm.innerHTML = confirmButtonText;
        box.className = 'ep-messgebox';
        titleBox.className = 'ep-messgebox-hd';
        body.className = 'ep-messgebox-bd ep-border-bottom';
        foot.className = 'ep-messgebox-fd';
        cancel.className = 'ep-messgebox-btn ep-messgebox-cancel';
        confirm.className = 'ep-messgebox-btn ep-messgebox-confirm';
        mask.className = 'ep-mask';
        document.body.appendChild(box);
        document.body.appendChild(mask);
        box.appendChild(titleBox);
        box.appendChild(body);
        box.appendChild(foot);
        foot.appendChild(cancel);
        foot.appendChild(confirm);
        return new Promise((resolve, reject) => {
            confirm.onclick = function () {
                document.body.removeChild(box);
                document.body.removeChild(mask);
                resolve(true);
            };
            cancel.onclick = function () {
                document.body.removeChild(box);
                document.body.removeChild(mask);
                resolve(false);
            };
        });
    },

    /**
     * actionsheet
     * @param {*} Objcet 
     */
    actionsheet(Objcet) {
        let box = document.createElement('div');
        let ul = document.createElement('ul');
        let header = document.createElement('div');
        let title = document.createElement('span');
        let cancel = document.createElement('div');
        let mask = document.createElement('div');
        box.className = 'ep-actionsheet';
        header.className = 'ep-header ep-border-bottom';
        cancel.className = 'ep-cancel';
        mask.className = 'ep-mask';
        document.body.appendChild(box);
        document.body.appendChild(mask);
        document.body.style.overflow = 'hidden';
        box.appendChild(header);
        box.appendChild(ul);
        header.appendChild(cancel);
        header.appendChild(title);
        title.innerHTML = Objcet.title || '';
        let str = ''
        let items = Objcet.items || [];
        items.forEach(item => {
            str += '<li class="ep-border-bottom">' + item.name + '</li>';
        });
        ul.innerHTML = str;
        box.style.height = items.length > 4 ? '2rem' : '';
        ul.style.height = items.length > 4 ? '1.6rem' : '';
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                box.style.transform = 'translateY(0%)';
            }, 1);
            let li = ul.getElementsByTagName("li");
            items.forEach((item, index) => {
                li[index].index = index;
                li[index].onclick = function () {
                    document.body.removeChild(box);
                    document.body.removeChild(mask);
                    document.body.style.overflow = '';
                    let obj = {
                        code: item.code,
                        item: item.name,
                        index: index
                    }
                    resolve(obj);
                }
            });
            cancel.onclick = function () {
                document.body.removeChild(box);
                document.body.removeChild(mask);
                document.body.style.overflow = '';
            };
        });
    },

    /**
     * 显示loading
     */
    showLoading() {
        let box = document.createElement('div');
        let loading = document.createElement('div');
        box.className = 'ep-dual-ring';
        loading.className = 'ep-circle';
        document.body.appendChild(box);
        box.appendChild(loading);
    },

    /**
     * 隐藏loading
     */
    hideLoading() {
        let box = document.querySelector('.ep-dual-ring');
        document.body.removeChild(box);
    }
}